package portal.controller;

import beans.HttpConfig;
import beans.TokenValue;
import net.HttpRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import portal.util.PaserPage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class HostGroupList extends HttpServlet {
    private static final long serialVersionUID = 2796970685901140947L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methodName = request.getParameter("method");
        Method method = null;
        try {
            method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * By黄梦阳 //TODO 按页数查询部分分组
     * @Param [request, response]
     * @return void
     **/
    protected void getPagedGroup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject pagedGrouplistResult = new JSONObject();
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        //发送请求拿到result
        if(!HttpConfig.fake){
            String  token = TokenValue.TOKEN.getTokenValue(HttpConfig.userName, HttpConfig.passWord);
            String resultStr = HttpRequest.sendGet(HttpConfig.hostGroupUrl,"token="+token);
            JSONObject result = JSONObject.fromObject(resultStr);
            if(result.getString("result").contains("success")){
                JSONArray hostgroup = result.getJSONArray("data");

                //分页
                JSONArray pagedGrouplist = PaserPage.returnCurrentDataList(currentPage,onePageCount,hostgroup);
                //总页数
                int totalPage = hostgroup.size()/onePageCount;
                if(hostgroup.size()%onePageCount != 0){
                    totalPage += 1;
                }
                JSONObject message = new JSONObject();
                message.put("pageNum",totalPage);
                message.put("pagedGrouplist",pagedGrouplist);
                pagedGrouplistResult.put("message",message);
                pagedGrouplistResult.put("result","success");
            }else{
                pagedGrouplistResult.put("message","目前没有分组");
                pagedGrouplistResult.put("result","false");
            }
        }else{//假数据
            String datastr = "{\"data\":[{\"GROUP_NAME\":\"节点分组1\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组2\",\"HOSTS\":\"wnode-sc\"},{\"GROUP_NAME\":\"节点分组3\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组4\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组5\",\"HOSTS\":\"wnode-sc\"},{\"GROUP_NAME\":\"节点分组6\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组7\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组8\",\"HOSTS\":\"wnode-sc\"},{\"GROUP_NAME\":\"节点分组9\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组10\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组11\",\"HOSTS\":\"all\"}]}";
            JSONObject data = JSONObject .fromObject(datastr);
            JSONArray hostgroup = data.getJSONArray("data");
            //分页
            JSONArray pagedGrouplist = PaserPage.returnCurrentDataList(currentPage,onePageCount,JSONArray.fromObject(hostgroup));
            //总页数
            int totalPage = hostgroup.size()/onePageCount;
            if(hostgroup.size()%onePageCount != 0){
                totalPage += 1;
            }
            JSONObject message = new JSONObject();
            message.put("pageNum",totalPage);
            message.put("pagedGrouplist",pagedGrouplist);
            pagedGrouplistResult.put("message",message);
            pagedGrouplistResult.put("result","success");
        }
        PrintWriter out = response.getWriter();
        out.write(pagedGrouplistResult.toString());
        out.flush();
        out.close();
    }

    /**
     * By黄梦阳 //TODO 新增作业时查询全部分组
     * @Param [request, response]
     * @return void
     **/
    protected void getAllGroup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject grouplistResult = new JSONObject();
        //发送请求拿到result
        if(!HttpConfig.fake){
            String  token = TokenValue.TOKEN.getTokenValue(HttpConfig.userName, HttpConfig.passWord);
            /**
             * {
             * "result":"success",
             * "message":"获取机器组成功。",
             * "data":[
             *          {"GROUP_NAME":"groupA","HOSTS":"all"},
             *          {"GROUP_NAME":"Linux","HOSTS":"all"},
             *          {"GROUP_NAME":"Windows","HOSTS":"wnode-sc"}
             *        ]
             * }
             */
            String resultStr = HttpRequest.sendGet(HttpConfig.hostGroupUrl,"token="+token);
            JSONObject result = JSONObject.fromObject(resultStr);
//            System.out.println(resultStr);
            if(result.getString("result").contains("success")){
                JSONArray hostgroup = result.getJSONArray("data");

                grouplistResult.put("message",hostgroup);
                grouplistResult.put("result","success");
            }else{
                grouplistResult.put("message","目前没有分组");
                grouplistResult.put("result","false");
            }
        }else{  //假数据
            String datastr = "{\"data\":[{\"GROUP_NAME\":\"节点分组1\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组2\",\"HOSTS\":\"all\"},{\"GROUP_NAME\":\"节点分组3\",\"HOSTS\":\"wnode-sc\"}]}";
            JSONObject data = JSONObject .fromObject(datastr);
            JSONArray hostgroup = data.getJSONArray("data");

            grouplistResult.put("message",JSONArray.fromObject(hostgroup));
            grouplistResult.put("result","success");
        }
        PrintWriter out = response.getWriter();
        out.write(grouplistResult.toString());
        out.flush();
        out.close();
    }

}
